package com.easytopit.demo.spring.mvc.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import com.easytopit.demo.spring.mvc.domain.Order;
import com.easytopit.demo.spring.mvc.domain.User;

@Controller
public class RequestControllerDemo {

	// @RequestMapping("/res/test")
	// public void paras(@RequestParam/**/(value="para", required=false) String
	// para,
	// @RequestHeader(value = "host", required = false) String host,
	// @CookieValue("JSESSIONID") String sessionId,
	// HttpServletResponse response, HttpServletRequest request, ModelMap
	// modelMap, ModelAndView mAndView) throws IOException {
	//
	// // @RequestParam request.getParameter("para") -> 默认
	// // @RequestHeader request.getHeader("host")
	// // @CookieValue request.getCookies()
	// // required 默认true
	//
	//// 注入 & 参数绑定(自动)
	//
	//
	// // 发送数据
	// response.getWriter().write("ok");
	// }

	// @RequestMapping("/res/test")
	// public String paras(@RequestParam/**/(value="para", required=false)
	// String para,
	// @RequestHeader(value = "host", required = false) String host,
	// @CookieValue("JSESSIONID") String sessionId,
	// HttpServletResponse response, HttpServletRequest request, ModelMap
	// modelMap, ModelAndView mAndView) throws IOException {
	//
	// // @RequestParam request.getParameter("para") -> 默认
	// // @RequestHeader request.getHeader("host")
	// // @CookieValue request.getCookies()
	// // required 默认true
	//
	//// 注入 & 参数绑定(自动)
	//
	//
	// // 发送数据
	//// response.getWriter().write("ok");
	// modelMap.put("name", "ssss");
	// return "hello";
	// }

	// @RequestMapping("/res/test")
	// public ModelAndView paras(@RequestParam/**/(value="para", required=false)
	// String para,
	// @RequestHeader(value = "host", required = false) String host,
	// @CookieValue("JSESSIONID") String sessionId,
	// HttpServletResponse response, HttpServletRequest request) throws
	// IOException {
	//
	// // @RequestParam request.getParameter("para") -> 默认
	// // @RequestHeader request.getHeader("host")
	// // @CookieValue request.getCookies()
	// // required 默认true
	//
	//// 注入 & 参数绑定(自动)
	//
	//
	// // 发送数据
	//// response.getWriter().write("ok");
	// ModelAndView modelAndView = new ModelAndView("hello");
	// modelAndView.addObject("name", "ssss");
	// return modelAndView;
	// }
	//
	// @RequestMapping("/res/test")
	// public ModelAndView paras(User user, Order order, Integer[] id) throws
	// IOException {
	//
	// // @RequestParam request.getParameter("para") -> 默认
	// // @RequestHeader request.getHeader("host")
	// // @CookieValue request.getCookies()
	// // required 默认true
	//
	//// 注入 & 参数绑定(自动)
	//
	//
	// // 发送数据
	//// response.getWriter().write("ok");
	// ModelAndView modelAndView = new ModelAndView("user");
	// modelAndView.addObject("user", user);
	// modelAndView.addObject("order", order);
	// return modelAndView;
	// }

	@RequestMapping("/res/user") // http://127.0.0.1:8080/res/user?id=2&name=3&userName=4
	public String parasObject(
			@ModelAttribute /* modelMap.addAttribute("user", user); */ User user, Order order, Integer[] id,
			ModelMap modelMap) throws IOException {
		modelMap.addAttribute("order", order);
		return "user";
	}

	@RequestMapping("/res/{id}-{name}")
	public void requestMapping(@PathVariable("id") Integer id, @PathVariable("name") String name,
			HttpServletResponse response) throws IOException {
		response.getWriter().write("id:" + id + " -> " + "name:" + name);
	}

	@RequestMapping("/res/render/{id}-{name}")
	public String requestMappingRender(@PathVariable("id") Integer id, @PathVariable("name") String name,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		modelMap.put("name", name);
		modelMap.put("id", id);
		return "render";
	}

	@RequestMapping("/res/rediect")
	public String redirect(HttpServletResponse response, ModelMap modelMap) throws IOException {
		// response.sendRedirect("/res/render/1-admin?name=x");
		return "redirect:/res/render/1-admin?name=x";
	}

	@RequestMapping("/res/forward")
	public String forward(HttpServletRequest request, HttpServletResponse response, HttpSession session,
			ModelMap modelMap, String name, @ModelAttribute("m") String m) throws IOException {
		session.setAttribute("name", name);
		modelMap.put("m", "3");
//		request.setAttribute("m", o);
//		throw new NullPointerException();
		return "forward:/res/forwardTo?m=1";
		// request.getRequestDispatcher("/res/render/1-admin?x=2&y=33").forward(request,
		// response);
	}

	@RequestMapping("/res/forwardTo")
	public void forwardTo(HttpServletResponse response, ModelMap modelMap, @SessionAttribute("name") String name,
			@ModelAttribute("m") String m) throws IOException {
		response.getWriter().write("OK");
	}
	
	@RequestMapping("/resx/**/s/*.do")
	public void resx(HttpServletResponse response) throws IOException {
		response.getWriter().write("OK");
	}
	
	
}
